package services;

import dao.ItemType_Repository;
import models.DatabaseExecutionContext;
import models.ItemType_T_Entity;
import play.db.jpa.JPAApi;

import javax.inject.Inject;
import java.util.List;
import java.util.concurrent.CompletionStage;

import static java.util.concurrent.CompletableFuture.supplyAsync;

public class ItemType_Repository_JPA implements ItemType_Repository{

    private final JPAApi jpaApi;
    private final DatabaseExecutionContext executionContext;

    @Inject
    public ItemType_Repository_JPA(JPAApi jpaApi, DatabaseExecutionContext executionContext) {
        this.jpaApi = jpaApi;
        this.executionContext = executionContext;
    }

    @Override
    public CompletionStage<List<ItemType_T_Entity>> getTypeProduct (String ItemType)
    {
        return supplyAsync(() -> {
            return jpaApi.withTransaction(entityManager -> {
                String strSql = "select * from dbo.f_GetItemType('"+ ItemType +"')";
                return entityManager.createNativeQuery(strSql, ItemType_T_Entity.class).getResultList();


            });
        }, executionContext);
    }
}
